package com.xuecheng.system.controller;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.Company;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    //机构查询
    @GetMapping("/company/mine")
    public ResponseResult<CompanyVo> findCompany(@RequestHeader("Authorization") String token) {
        //校验签名合法性token
        //Boolean verifyToken = JwtUtils.verifyToken(token);
        //if (verifyToken == false) {
        //   throw new BusinessException(ErrorCode.NOTTOKEN);
        //}
        //解析token,以下是自定义方法，不是系统方法
        //AuthInfo authInfo = JwtUtils.getInfoFromToken(token);

        //从threadlocal获取公司信息
        AuthInfo authInfo = AuthInfoHolder.getAuthInfo();
        //用户类别普通，也就是用户没有企业
        if (authInfo.getCompanyId() == null) {
            throw new BusinessException(ErrorCode.COMPANYERROR);
        }
        //根据企业id查询企业对象
        Company company = companyService.getById(authInfo.getCompanyId());
        //封装返回
        CompanyVo companyVo = BeanHelper.copyProperties(company, CompanyVo.class);
        return ResponseResult.okResult(companyVo);
    }


    //根据id查询企业
    @GetMapping("/company/{id}")
    public ResponseResult<CompanyVo> find(@PathVariable("id") Long companyId) {
        return companyService.find(companyId);
    }
}
